package com.guoqi.myclientdemo

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {

    val TAG = this.javaClass.simpleName

    private var isConn = false
    private var shoppingManager: ShoppingManager? = null
    private var localGoods = ArrayList<Good>()

    private val serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            tv_conn.text = "客户端 ---> 服务端  已连接"
//           var message = Messenger(service) 也可以用Message来传递消息
            shoppingManager = ShoppingManager.Stub.asInterface(service)
            isConn = true
        }

        override fun onServiceDisconnected(name: ComponentName) {
            tv_conn.text = "客户端 ---> 服务端  未连接"
            isConn = false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initGood()

        btn_conn.setOnClickListener {
            if (!isConn) {
                startConn()
                btn_conn.text = "断开连接"
            } else {
                stopConn()
                btn_conn.text = "连接服务端"
            }
        }
        tv_clear.setOnClickListener {
            tv_log.text = "暂无输出内容"
        }
        btn_add.setOnClickListener {
            addGood()
        }
        btn_find.setOnClickListener {
            findGood()
        }
        btn_del.setOnClickListener {
            delGood()
        }
    }

    private fun initGood() {
        var good1 = Good()
        good1.name = "白菜"
        good1.price = 2.51
        localGoods.add(good1)

        var good2 = Good()
        good2.name = "胡萝卜"
        good2.price = 3.2
        localGoods.add(good2)

        var good3 = Good()
        good3.name = "西红柿"
        good3.price = 4.0
        localGoods.add(good3)

        var good4 = Good()
        good4.name = "土豆"
        good4.price = 1.8
        localGoods.add(good4)
    }


    private fun addGood() {
        if (isConn && shoppingManager != null) {
            //随机添加一个
            var i = Random().nextInt(localGoods.size)
            shoppingManager?.addGood(localGoods[i])
            findGood()
        } else {
            Toast.makeText(this@MainActivity, "未连接服务端", Toast.LENGTH_LONG).show()
        }
    }

    private fun findGood() {
        if (isConn && shoppingManager != null) {
            var goodList = shoppingManager?.goods as ArrayList
            var result = StringBuilder()
            for (good in goodList) {
                result.append(good.toString() + "\n")
            }
            result.append("----------------------" + "\n")
            tv_log.text = result
        } else {
            Toast.makeText(this@MainActivity, "未连接服务端", Toast.LENGTH_LONG).show()
        }

    }

    private fun delGood() {
        if (isConn && shoppingManager != null) {
            var goodList = shoppingManager?.goods as ArrayList
            //删除最后一个
            if (goodList.isNotEmpty()) {
                shoppingManager?.delGood(goodList[goodList.size - 1])
                findGood()
            } else {
                Toast.makeText(this@MainActivity, "没有商品了", Toast.LENGTH_LONG).show()
            }
        } else {
            Toast.makeText(this@MainActivity, "未连接服务端", Toast.LENGTH_LONG).show()
        }
    }

    private fun startConn() {
        val intent = Intent()
        intent.setPackage("com.guoqi.myserverdemo")
        intent.action = "test_aidl"
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
    }

    private fun stopConn() {
        if (isConn) {
            unbindService(serviceConnection)
            isConn = false
            tv_conn.text = "客户端 ---> 服务端  未连接"
        }
    }
}
